<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\common\facade\Builder;
use think\facade\View;
//该文件为自动生成，请按需修改
class Profession extends Base
{
    //所有定义变量均为演示，请根据实际需求更改

    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\Profession';

    //关联模型
    protected array $hasOne = [];

    //附加字段或者虚拟字段
    protected array $append = [];

    //数据排序，支持多个
    protected array $sort = ['sort' => 'asc'];

    //是否主模型接管getList，1完全接管，2对象接管，3数据接管
    protected int $takeOver = 0;

    //是否查询已删除的数据，默认0不查询，如需查询请设置1，如全部设置请勿定义此变量
    protected int $is_del = 0;

    //是否写入日志：0不写入，1写入，2自定义写入；默认1
    //如果给定 2 自定义写入，则需要在主Model定义writeLog(静态)方法
    //如果给定 1 自动写入日志，无需定义此变量
    protected int $writeLog = 1;

    //日志描述匹配，默认会匹配title(名称)和code(编号)，如果没有此字段请给定唯一描述标识
    //如果有title或者code字段，无需定义此变量，否则只会记录标识ID
    protected array $writeLogKey = ['key' => 'title,code', 'val' => '名称,编号'];
    //工种管理
    public function work(): string
    {
        return $this->setTable('工种名称',[['type', '=', 1]]);
    }
    //车型管理
    public function car_type():string{
        
        return $this->setTable('车型名称',[['type', '=', 2]]);
    }
    //车辆品牌管理
    public function car_brand():string{
        
        return $this->setTable('车辆品牌名称',[['type', '=', 3]]);
    }
    public function setTable($title,$extra_map = []): string
    {
        //print('<pre>');print_r($extra_map);die;
        //构建表格数据
        $table =  Builder::Table()
            ->addColumn(['type' => 'checkbox', 'title' => '全选', 'fixed' => 'left'])
            ->addColumn(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addColumn(['field' => 'title', 'title' => $title, 'modify' => '2,input'])
            ->addColumn(['field' => 'sort', 'title' => '排序', 'modify' => '2,input', 'templet' => '#sort_sort', 'width' => 120])
            ->addColumn(['field' => 'create_time', 'title' => '创建时间'])
            ->addColumn(['field' => 'update_time', 'title' => '更新时间'])
            ->addColumn(['fixed' => 'right', 'title' => '操作', 'align' => 'center', 'toolbar' => '#barDemo', 'width' => 200])
            ->addRightButtons(['edit' => ['size' => '1000,1000'], 'delete'])
            ->addTopButtons([
                'add' => [
                        'size' => '1000,1000',
                        'href'   => url('add', ['type'=>$extra_map[0][2],'title'=>$title])->build(),
                        'event' => '_openNewPage'
                ], 
                'deleteAll'
            ]);
        return $table->addSearch([
                ['title', $title.'搜索', 'input', '', '', '', 'like']
            ])
            ->addExtraMap($extra_map)
            ->addSortTool([['name' => 'sort', 'pk' => $this->pk, 'url' => url('update')->build()]])
            ->fetch();
    }
    
    public function add($tpl = 'profession/add')
    {
        $request=$this->request->get();
        View::assign('type', $request['type']);
        View::assign('title', $request['title']);
        View::assign('sort', ($this->model::where(['is_del'=>0,'type'=>$request['type']])->max('sort')+1));
        return parent::add($tpl);
    }

}
